﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using ExtAspNet;
using System.Data;
using Ylh.BLL;

public partial class Module_DepartmentManger_AddDepartment : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        BtnCancel.OnClientClick = ActiveWindow.GetHidePostBackReference();
        StaffName.OnClientTriggerClick = Window1.GetSaveStateReference(StaffID.ClientID, Tel.ClientID, StaffName.ClientID) + Window1.GetShowReference("../StaffManger/StaffTree.aspx", "选择负责人");

        if (!IsPostBack)
        {
            if (Request.QueryString["DepartmentID"] != null)
            {
                FillData(Request.QueryString["DepartmentID"].ToString());
            }

        }
    }

    private void FillData(string DepartmentsID)
    {
        Ylh.BLL.DepartmentBLL debll = new DepartmentBLL(DbConnectionStr);
        DataTable dt = debll.GetDepartMentInfo(Convert.ToDecimal(DepartmentsID));
        txtDepartmentName.Text = dt.Rows[0]["DepartmentName"].ToString();
        txtTeamName.Text = dt.Rows[0]["TeamName"] != null ? dt.Rows[0]["TeamName"].ToString() : "";
        StaffName.Text = dt.Rows[0]["StaffName"] != null ? dt.Rows[0]["StaffName"].ToString() : "";
        txtSlogan.Text = dt.Rows[0]["Slogan"] != null ? dt.Rows[0]["Slogan"].ToString() : "";
        StaffID.Text = dt.Rows[0]["StaffID"] != null ? dt.Rows[0]["StaffID"].ToString() : "";
        DepartmentID.Text = dt.Rows[0]["DepartmentID"].ToString();
        txtRemark.Text = dt.Rows[0]["Remark"] != null ? dt.Rows[0]["Remark"].ToString() : "";
        IsServer.Checked = (dt.Rows[0]["IsServer"] != null || dt.Rows[0]["IsServer"]!=DBNull.Value) ? Convert.ToBoolean(dt.Rows[0]["IsServer"]) : false;
    }

    private Ylh.Model.TbDepartment GetInfo()
    {
        string DepartmentName = this.txtDepartmentName.Text;
        string Slogan = this.txtSlogan.Text;
        string TeamName = this.txtTeamName.Text;
        string Remark = this.txtRemark.Text;
        int StaffID = -1;
        if (!string.IsNullOrEmpty(this.StaffID.Text))
        {
            StaffID = int.Parse(this.StaffID.Text);
        }


        Ylh.Model.TbDepartment model = new Ylh.Model.TbDepartment();
        model.DepartmentName = DepartmentName;
        model.Slogan = Slogan;
        model.TeamName = TeamName;
        model.Remark = Remark;
        model.StaffID = StaffID;
        model.IsServer = IsServer.Checked;
        return model;
    }




    protected void BtnSave_Click(object sender, EventArgs e)
    {
        DepartmentBLL debll = new DepartmentBLL(DbConnectionStr);

        if (this.txtDepartmentName.Text == "")
        {
            Alert.Show("部门名称不能为空");
            return;
        }
        Ylh.Model.TbDepartment model = GetInfo();
        if (string.IsNullOrEmpty(DepartmentID.Text.Trim()))
        {
            if (debll.IsDeParNameExists(txtDepartmentName.Text.Trim()))
            {
                Alert.Show("部门名称已经存在,请重新输入");
                return;
            }
            DepartmentID.Text = debll.InsertDepartment(model).ToString();
            Alert.Show("部门信息创建成功");
            txtDepartmentName.Enabled = false;
        }
        else
        {
            model.DepartmentID = Convert.ToInt32(DepartmentID.Text);
            debll.UpdateDepartment(model);
            Alert.Show("部门信息修改成功");

        }
        FillData(DepartmentID.Text.Trim());
    }
}